package pres.seanlang.utility.android;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import pres.seanlang.utility.ReflectUtils;

/**
 *  关于System的工具集
 *
 * @author SeanLang
 */
public class ASysUtils {
    private ASysUtils() { throw new UnsupportedOperationException("can't instance..."); }

    /**
     *  获取显示屏幕的metrics
     * @param activity
     * @return
     */
    public static DisplayMetrics getDisplayMetrics(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        if(activity != null) {
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        }

        return metrics;
    }

    /**
     *  获取app packageinfo
     * @param context
     * @return
     */
    public static PackageInfo getPackageInfo(Context context) {
        PackageManager manager = context.getPackageManager();
        if(manager != null) {
            try {
                return manager.getPackageInfo(context.getPackageName(), 0);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    /**
     *  获取版本名
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {
        PackageInfo packageInfo = getPackageInfo(context);
        if(packageInfo != null) {
            return packageInfo.versionName;
        } else {
            return "";
        }
    }

    /**
     *  获取版本号
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        PackageInfo packageInfo = getPackageInfo(context);
        if(packageInfo != null) {
            return packageInfo.versionCode;
        } else {
            return 0;
        }
    }

    public static String getSerialno() {
        return (String) ReflectUtils.invoke("android.os.SystemProperties", "get", "ro.serialno");
    }

    public static int getMaxVolume(Context context) {
        AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int volume = manager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        return volume;
    }

    public static String getAndroidID(Context context) {
        String id = null;
        if(context == null) {
            ContentResolver resolver = context.getContentResolver();
            id = Settings.Secure.getString(resolver, Settings.Secure.ANDROID_ID);
            //if ("9774d56d682e549c".equals(id)) return "";
            if("9774d56d682e549c".equals(id)) {
                id = "";
            }
        }
        return id == null ? "" : id;
    }
}
